<?php
/**
 * 公共
 *
 * @author lwb
 */

namespace app\wechat\controller;

class Common extends \think\Controller {

    public $uid;
    public function __construct() {
        parent::__construct();
        $this->uid = session('uid');

        // 获取学员状态
        $userStatus = $this->getWS('ucenter', 'Users@userStatus', ['userName'=>session('username')]);
        if($userStatus['status'] == '2000'){
            if($userStatus['result']['u_status'] != 1){
                session(null);
                return redirect('/payIndex');
            }
        }
    }
    
    /*
     * ESB请求
     */
    protected function getWS($sysSign, $interface, $params) {
        $s = new \SoapClient(config('esb.wsdl'));
        $soap_var = new \SoapVar(['authName' => config('esb.auth_name'), 'authPwd' => config('esb.auth_pwd')], SOAP_ENC_OBJECT, 'auth', 'mayi');
        $u = new \SoapHeader('mayi', 'auth', $soap_var, true);
        $s->__setSoapHeaders($u);
        $suc = $s->destInterface(config('esb.sys_sign'), $sysSign, $interface, json_encode($params));
        return json_decode($suc, true);
    }

    /*
     * 统一返回调用
     */
    protected function parse_data($result = []) {
        $tm = time();
        $retdata = [
            'status' => isset($result['code']) ? $result['code'] : 2000,
            'msg' => isset($result['msg']) ? $result['msg'] : '操作成功',
            'result' => isset($result['data']) ? $result['data'] : [],
            'runtime' => $tm,
        ];
        return json($retdata);
    }
    
    /*
     * 检查订单状态
     */
    protected function checkOrder(){
        if(!session('b_id') && !session('partner_id')){
            return true;
        }
        
        if(session('b_id')){
            $result = $this->getWS('credit', 'apply@applyStatus', ['b_id' => session('b_id')]);
            if(!in_array($result['result']['status'], ['0000','1000'])){
                return true;
            }
        }
        
        return false;
    }

    /**
     * 支持数组的addslashes(接受参数过滤空格，防止sql攻击)
     * @param hanming
     * @param mixed $string
     * @return unknown
     */
    protected function xaddslashes(&$string)
    {
        if (!ini_get('magic_quotes_gpc')) {
            if (is_array($string)) {
                if (!empty($string)) {
                    foreach ($string as $key => $val) {
                        $val = trim($val);
                        if (!empty($val)) {
                            $string[ $key ] = self::xaddslashes($val);
                        }
                    }
                }
            } elseif ($string == null || empty($string)) {
                //null
            } else {
                $string = addslashes($string); //addslashes转义sql语句, htmlspecialchars转义<和>(防止构造php和js语句)
            }
        }

        return $string;
    }

    /**
     * 系统拒单给学员和老师发送信息通知
     * @param lihuifeng
     * @param  array
     * @return bool
     */
    protected function sendOrderSms($u_phone,$u_true_name,$partner_name,$partner_contact_phone,$reason)
    {
        //通知学员
        $sendMsg = [
            'tplName' => 'F003',
            'phone' => $u_phone,
            'param' => ['partnername'=>$partner_name,'reason' => $reason]
        ];
        $this->getWS('channel', 'message@send', $sendMsg);

        //通知机构
//        $sendMsg = [
//            'tplName' => 'F003',
//            'phone' => $partner_contact_phone,
//            'param' => ['partnername'=>$partner_name,'reason' => $reason]
//        ];
        $sendMsg = [
            'tplName' => 'PA002',
            'phone' => $partner_contact_phone,
            'param' => ['uname'=>$u_true_name,'reason' => $reason]
        ];
        $this->getWS('channel', 'message@send', $sendMsg);
    }

    /**
     * Desc: 系统通过给学员发送信息通知
     * User: liuning
     * Date: 2019/10/14 17:02
     * Return: array
     * Update:
     */
    protected function sendSuccOrderSms($u_phone,$partner_name)
    {
        //通知学员
        $sendMsg = [
            'tplName' => 'K_F001',
            'phone' => $u_phone,
            'param' => ['partnername'=>$partner_name]
        ];
        $this->getWS('channel', 'message@send', $sendMsg);
    }

    /**
     * Desc: 提醒审单人员
     * User: liuning
     * Date: 2019/10/14 17:02
     * Return: array
     * Update:
     */
    protected function sendExOrderSms()
    {
        //周六日进行短信提醒
        if((date('w')==6) || (date('w') == 0)){
            $sendMsg = [
                'tplName' => 'SD001',
                'phone' => '13488812881',
                'param' => ['name'=>'王美容','date'=>date('Y-m-d H:i:s',time())]
            ];
            $this->getWS('channel', 'message@send', $sendMsg);
        }
    }

    /**
     * Desc: 检测OCR人脸识别次数
     * User: liuning
     * Date: 2019/9/20 18:22
     * Return: array
     * Update:
     */
    public function checkOcrNum($params){
//        $params = array(
//            'u_code'=>  'U19011468107',
//            'u_name'=>  '唐柒柒',
//            'u_from'=> 1,
//            'u_type'=> 1,
//            'add_time'=> 1,
//            'partner_id'=> '318',
//        );

        //机构如果
        if(empty($params['u_code'])){
            return $this->parse_data(['code' => 5000, 'msg' =>'参数错误']);
        }

        $checkData = array(
            'u_code'=>  $params['u_code'],
            'u_b_id'=>  $params['u_b_id'],
            'u_from'=> 2,
            'add_time'=> 1,//特殊处理，如果有任意值，则暂时区别于空号检测，不加一小时内的限定条件
            'u_status'=> 6,
        );

        //查询ocr身份检测次数
        $checkNum = $this->getWS('ucenter', 'users@userChecknumInfo', $checkData);
        $numCount = count($checkNum['result']);

//        $countLog = [
//            'desc' =>'ocr检测次数返回',
//            'count' =>$numCount.'次',
//            'result' =>$checkNum,
//        ];
//        Log::write($countLog, 'log', true, 'ocrcard/');

        //机构设置的检测次数
        if($params['partner_id']){
            $ocr_num = db("partners")->field('partner_ocr_num')->where("partner_id = '{$params['partner_id']}'")->find();
            $partner_ocr_num = $ocr_num['partner_ocr_num'];
        }else{
            $partner_ocr_num = 1; //暂定为1次(与数据库默认一致)
        }

        //当校验次数为最后一次时，弹出提示 1：弹出；2：不弹出
        $last_pop = 2;
        if(($numCount+1) == $partner_ocr_num){
            $last_pop = 1;
        }

        if($numCount >= $partner_ocr_num){
//            return $this->parse_data(['code' => 5001, 'msg' => '人脸校验超限，您的认证将通过人工审核，请重新上传！']);
            return ['code' => 5000, 'data' => $checkNum['result'], 'msg' => '人脸校验超限，您的认证将通过人工审核，请重新上传！'];
        }
//        return $this->parse_data(['code' => 2000, 'msg' => '人脸校验成功！']);
        return ['code' => 2000, 'last_pop'=> $last_pop, 'msg' => '人脸校验成功！'];
    }

}
